package org.jxht.web.api;

import org.jxht.dto.TeacherDto;
import org.jxht.service.TeacherService;
import org.jxht.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by huang on 2017/3/20 0020.
 */
@Controller
@RequestMapping("JxhtWeb/TeacherController")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 添加教师用户
     * @param reuqestId 执行该功能的用户Id
     * @param requestJson Json字符串
     * @return String
     */
    @RequestMapping(value = "/{reuqestId}/addTeacherInfo")
    public String addTeacherInfo(@PathVariable(value = "reuqestId")int reuqestId,
                                 @RequestParam(value = "requestJson")String requestJson){
        List<TeacherDto> dtos = GsonUtil.parseJsonArrayWithGson(requestJson,TeacherDto.class);
        for(TeacherDto dto:dtos){
            teacherService.insertTeacherService(dto);
        }
        return "SUCCESS";
    }

    /**
     * 更新教师信息
     * @param reuqestId 执行该功能的用户Id
     * @param requestJson Json字符串
     * @return String
     */
    @RequestMapping(value = "/{reuqestId}/changeTeacherInfo")
    public String changeTeacherInfo(@PathVariable(value = "reuqestId")int reuqestId,
                                 @RequestParam(value = "requestJson")String requestJson){
        List<TeacherDto> dtos = GsonUtil.parseJsonArrayWithGson(requestJson,TeacherDto.class);
        for(TeacherDto dto:dtos){
            teacherService.updataTeacherService(dto);
        }
        return "SUCCESS";
    }

    /**
     * 删除教师信息
     * @param requestId 教师编号
     * @return
     */
    @RequestMapping(value = "/{requestId}/deleteTeacherInfo")
    public String deleteTeacherInfo(@PathVariable(value = "requestId")int requestId){
        teacherService.deleteTeacherService(new TeacherDto(requestId));
        return "SUCCESS";
    }

    /**
     * 获取教师信息
     * @param requestId 课程号、教师课程记录号、系号
     * @param requestWay 请求方法
     * @return
     */
    @RequestMapping(value = "/{requestId}/getTeacherInfo")
    public List<TeacherDto> getTeacherInfo(@PathVariable(value = "requestId")int requestId,
                                           @RequestParam(value = "requestWay")String requestWay){
        return teacherService.queryTeacherService(requestId,requestWay);
    }
}
